package com.itheima.health.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    PermissionService permissionService;
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {

        //rpc查询结果
        PageResult pageResult = permissionService.pageQuery(queryPageBean);
        //构造返回对象
        return new Result(true, MessageConst.ACTION_SUCCESS,pageResult);

    }


    @PostMapping("/add")
    public Result add(@RequestBody Permission permission) {
        //RPC请求添加
        permissionService.add(permission);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    @PostMapping ("/edit")
    public Result edit(@RequestBody Permission permission) {
        //RPC请求添加
        permissionService.edit(permission);
        return new Result(true, MessageConst.ACTION_SUCCESS);

    }


    @RequestMapping("/findById")

    public Result findById(Integer id) {
        log.info("[检查项-根据ID查询]id:{}", id);
        Permission permission = permissionService.findById(id);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }


    @RequestMapping("/delete")
    public Result delete(Integer id){
        log.info("[检查项-根据id删除]id：{}",id);
        //RPC调用处理业务
        permissionService.delete(id);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }
    @GetMapping("/findAll")
    public Result findAll(){
        List<Permission> all = permissionService.findAll();
        return new Result(true, MessageConst.ACTION_SUCCESS,all);
    }


}
